package com.cskaoyan.service.wx;

import com.cskaoyan.bean.Footprint;
import com.cskaoyan.bean.Goods;
import com.cskaoyan.bean.wx.WxFootprintFinalVO;
import com.cskaoyan.bean.wx.WxFootprintVO;
import com.cskaoyan.mapper.FootprintMapper;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.utils.IdUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WxFootprintServiceImpl implements WxFootprintService {

    @Autowired
    FootprintMapper footprintMapper;
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public WxFootprintFinalVO queryWxFootprintList(Integer page, Integer size) {
        //获取userId
        Integer userId = IdUtils.getId();
        //分页
        PageHelper.startPage(page, size);

        List<Footprint> footprints = footprintMapper.selectByUserId(userId);
        ArrayList<WxFootprintVO> wxFootprintVOList = new ArrayList<>();
        for (Footprint footprint : footprints) {
            WxFootprintVO wxFootprintVO = new WxFootprintVO();
            //封装addTime goodsId
            wxFootprintVO.setAddTime(footprint.getAddTime());
            wxFootprintVO.setGoodsId(footprint.getGoodsId());
            wxFootprintVO.setId(footprint.getId());
            //封装brief picurl等
            Goods goods = goodsMapper.selectByPrimaryKey(footprint.getGoodsId());
            wxFootprintVO.setBrief(goods.getBrief());
            wxFootprintVO.setPicUrl(goods.getPicUrl());
            wxFootprintVO.setName(goods.getName());
            wxFootprintVO.setRetailPrice(Double.parseDouble(goods.getRetailPrice().toString()));
            wxFootprintVOList.add(wxFootprintVO);
        }
        //总页数
        PageInfo pageInfo = new PageInfo(footprints);
        long total = pageInfo.getTotal();

        WxFootprintFinalVO wxFootprintFinalVO = new WxFootprintFinalVO();
        wxFootprintFinalVO.setFootprintList(wxFootprintVOList);
        wxFootprintFinalVO.setTotalPages(total);


        return wxFootprintFinalVO;
    }

    @Override
    public void deleteWxFootprint(Integer id) {
        int i = footprintMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insertFootprint(Integer id, Integer userId) {
        Footprint footprint = new Footprint();
        footprint.setAddTime(new Date());
        footprint.setDeleted(false);
        footprint.setUpdateTime(new Date());
        footprint.setGoodsId(id);
        footprint.setUserId(userId);

        footprintMapper.insertSelective(footprint);
    }
}
